package com.lp.biz.operations.controller;

import com.lp.biz.operations.model.vo.TaskCreate;
import com.lp.biz.operations.service.TaskService;
import com.lp.biz.train.model.vo.ClassModerator;
import com.lp.common.annotation.PermissionAnnotation;
import com.lp.common.entity.PageParams;
import com.lp.common.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.lp.common.constant.CommonConstant.NEGATE;
import static com.lp.common.constant.CommonConstant.SURE;
import static com.lp.common.constant.MenuConstant.CONTENT_CLASS;
import static com.lp.common.constant.MenuConstant.TASK_MANAGE;
import static com.lp.common.constant.RoleConstant.CLASS_TEACHER;

/**
 * @program: backend
 * @description:
 * @author: Ke.Song
 * @since: 2024-08-06 20:58:53
 */
@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    private TaskService taskService;

    /**
     * 获取申请活动列表
     * @param query 查询参数
     * @return 查询结果
     */
    @GetMapping("/apply")
    @PermissionAnnotation(menus = {TASK_MANAGE})
    public CommonResult getTaskApplyList(PageParams query) {
        return taskService.getTaskApplyList(query);
    }

    /**
     * 更换申请活动状态
     * @param applyId 申请id
     * @return 处理结果
     */
    @PutMapping("/apply/{applyId}")
    @PermissionAnnotation(menus = {TASK_MANAGE})
    public CommonResult updateTaskApplyStatus(@PathVariable Integer applyId) {
        taskService.updateTaskApplyStatus(applyId);
        return CommonResult.success();
    }

    /**
     * 通过活动申请
     * @param applyId 申请id
     * @return 处理结果
     */
    @PutMapping("/{applyId}/state/agree")
    @PermissionAnnotation(menus = {TASK_MANAGE})
    public CommonResult agreeTaskApplyState(@PathVariable Integer applyId) {
        taskService.updateTaskApplyState(applyId, SURE);
        return CommonResult.success();
    }

    /**
     * 拒绝活动申请
     * @param applyId 申请id
     * @return 处理结果
     */
    @PutMapping("/{applyId}/state/refuse")
    @PermissionAnnotation(menus = {TASK_MANAGE})
    public CommonResult refuseTaskApplyStatus(@PathVariable Integer applyId) {
        taskService.updateTaskApplyState(applyId, NEGATE);
        return CommonResult.success();
    }

    /**
     * 创建活动
     * @param taskCreate 活动参数
     * @return 处理结果
     */
    @PostMapping("")
    @PermissionAnnotation(menus = {TASK_MANAGE})
    public CommonResult addTask(@RequestBody @Validated TaskCreate taskCreate) {
        taskService.addTask(taskCreate);
        return CommonResult.success();
    }

    /**
     * 获取活动列表
     * @param query 查询参数
     * @return 查询结果
     */
    @GetMapping("")
    @PermissionAnnotation(menus = {TASK_MANAGE})
    public CommonResult getTasks(PageParams query) {
        return taskService.getTasks(query);
    }

    /**
     * 获取活动详情
     * @param taskId 活动ID
     * @return 查询结果
     */
    @GetMapping("/{taskId}")
    @PermissionAnnotation(menus = {TASK_MANAGE})
    public CommonResult getTaskDetail(@PathVariable Integer taskId) {
        return CommonResult.success();
    }

    /**
     * 获取活动人员列表
     * @param taskId 活动ID
     * @param query 查询参数
     * @return 查询结果
     */
    @GetMapping("/{taskId}/members")
    @PermissionAnnotation(menus = {TASK_MANAGE})
    public CommonResult getMembers(@PathVariable Integer taskId, PageParams query) {
        return taskService.getMembers(taskId, query);
    }

    /**
     * 设置活动主持人
     * @param taskId 活动id
     * @param moderator 主持人
     * @return 处理结果
     */
    @PutMapping("/{taskId}/moderator")
    @PermissionAnnotation(menus = {CONTENT_CLASS}, roles = {CLASS_TEACHER})
    public CommonResult updateModerator( @PathVariable Integer taskId, @RequestBody@Validated ClassModerator moderator) {
        taskService.updateModerator(taskId, moderator);
        return CommonResult.success();
    }

    /**
     * 参与/未参与活动
     * @return 查理结果
     */
    @PutMapping("/{taskId}/join/{memberId}")
    public CommonResult updateJoin(@PathVariable Integer memberId, @PathVariable Integer taskId) {
        taskService.updateJoin(memberId, taskId);
        return CommonResult.success();
    }

    /**
     * 活动点评/未点评
     * @param memberId 用户id
     * @param taskId 活动id
     * @return 处理结果
     */
    @PutMapping("/{taskId}/review/{memberId}")
    public CommonResult updateReview(@PathVariable Integer memberId, @PathVariable Integer taskId) {
        taskService.updateReview(memberId, taskId);
        return CommonResult.success();
    }

}
